﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;
using System.Configuration;

namespace Ruandao.HostServer
{
    public class FixedHostServerContext : HostServerContext
    {
        SiteWare siteware = null;

        public FixedHostServerContext()
        {
            // empty
        }

        internal override SiteWare GetCurrentSiteWare()
        {
            if (siteware == null)
            {
                Site site = new Site();
                var conn = ConfigurationManager.ConnectionStrings["SiteDatabase"];
                if (conn != null)
                {
                    site.ConnectionString = conn.ConnectionString;
                    site.ProviderName = conn.ProviderName;
                }
                site.StorageLocation = ConfigurationManager.AppSettings["SiteFolder"];

                Ware ware = new Ware();
                ware.WareName = GetCurrentWareName();
                ware.DynamicPathMaps = new Dictionary<string, string>();
                ware.Port = "8000";

                siteware = new SiteWare();
                siteware.Site = site;
                siteware.Ware = ware;
                siteware.AccountContextName = ConfigurationManager.AppSettings["SiteWareAccountContextName"];
                var wareconn = ConfigurationManager.ConnectionStrings["SiteWareDatabase"];
                if (wareconn != null)
                {
                    siteware.ConnectionString = wareconn.ConnectionString;
                    siteware.ProviderName = wareconn.ProviderName;
                }
            }
            return siteware;
        }

        static private string GetCurrentWareName()
        {
            string name = ConfigurationManager.AppSettings["SitewareName"];
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new Exception("必须在appSettings中使用SitewareName名称来设置网件的名称。");
            }
            return name;
        }
    }
}
